<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
    
    function __construct()
    {
        parent :: __construct();
        $this->lang->load('user', 'chinese');
        $this->load->model('User_model');
    }

    public function index()
    {
        if($this->_isnot_login())
        {
            $this->load->view('user');
        }
        else
        {
            redirect('');
        }
    }
    
    public function doLogin()
    {
        $validation = array(
            array(
                'field' => 'email',
                'label' => lang('email'),
                'rules' => 'trim|required|valid_email'
            ),
            array(
                'field' => 'password',
                'label' => lang('password'),
                'rules' => 'trim|required|min_length[5]|max_length[20]'
            ),
        );

        // Set the validation rules
        $this->form_validation->set_rules($validation);
        $user_data = array();
        // Convert the array to an object
        $user_data['email']			= $this->security->xss_clean($this->input->post('email'));
        $user_data['password'] 			= $this->security->xss_clean($this->input->post('password'));
        
        if ($this->form_validation->run())
        {
            if($user = $this->User_model->isLogin($user_data))
            {
                //设置SESSION
                $this->session->set_userdata($user);
                $this->goback(1);
            }
            else
                $this->goback(0);
        }
        else
        {
            echo validation_errors(); 
        }
    }
    
    function logout() {
        $this->session->sess_destroy();
        redirect('');
    }
}